//
//  SearchPartnerTableViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/22.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "SearchPartnerTableViewController.h"
#import "BusinessTableViewCell.h"
#import "CommitAddPartnerViewController.h"
#import "VervifyPhone.h"
#import <MBProgressHUD.h>
#import "SearchNet.h"
#import "BLHeader.h"

@interface SearchPartnerTableViewController ()<UITextFieldDelegate>

@property (nonatomic, strong) NSArray *partners;

@end

@implementation SearchPartnerTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.searchController.searchBar.placeholder = @"请输入手机号码";
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.searchController.searchBar.placeholder = @"搜索";
}

- (void)setupUI {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"BusinessTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ReuseIdentify];
    self.searchController.searchBar.placeholder = @"请输入手机号码";
    
   [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName:kAppThemeColor} forState:UIControlStateNormal];
}


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

}

#pragma mark - UISearchBarDelegate
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    //_searchVC.friendsArray = self.friendsArray;
    [self.tabBarController.tabBar setHidden:YES];
}

- (void) searchBarCancelButtonClicked:(UISearchBar *)searchBar {
   // [self.tabBarController.tabBar setHidden:NO];
    self.searchController.searchBar.placeholder = @"搜索";
}


-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    // 修改UISearchBar右侧的取消按钮文字颜色及背景图片
    //_searchController.searchBar. = [UIColor whiteColor];
    for (id searchbuttons in [[searchBar subviews][0] subviews]) //只需在此处修改即可
        if ([searchbuttons isKindOfClass:[UIButton class]]) {
            UIButton *cancelButton = (UIButton*)searchbuttons;
            // 修改文字颜色
            [cancelButton setTintColor:kAppThemeColor];
        }
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchBarCancelButtonClicked:searchBar];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    NSString *tel = searchBar.text;
    if([tel length] == 11 && [VervifyPhone vervifyTellphoneNumber:tel]) {
        __weak typeof (self) weakSelf = self;
        [SearchNet searchPartner:tel completer:^(id result, NSError *error) {
            if(result) {
                [hud hideAnimated:YES];
                weakSelf.partners = result;
                [weakSelf.tableView reloadData];
            }else {
                hud.mode = MBProgressHUDModeText;
                hud.label.text = [error localizedDescription];
                [hud hideAnimated:YES afterDelay:2];
            }
        }];
    }else {
        hud.label.text = @"请输入规范的手机号码";
        hud.mode = MBProgressHUDModeText;
        [hud hideAnimated:YES afterDelay:2];
    }
    [self.view endEditing:YES];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.partners count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BusinessTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReuseIdentify];
    [cell setupCell:self.partners[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CommitAddPartnerViewController *commit = [[CommitAddPartnerViewController alloc] init];
    commit.partner = self.partners[indexPath.row];
    [self.navigation pushViewController:commit animated:YES];
    self.searchController.searchBar.text = @"";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}

@end
